package com.cxm.helper.service.impl;

import cn.hutool.core.util.IdUtil;
import com.aliyun.oss.*;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.VoidResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: chenximeng
 * @date: 2022/11/19 09:54
 */
@Service
public class AliyunOssService {

    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;
    @Value("${aliyun.key-id}")
    private String accessKeyId;
    @Value("${aliyun.key-secret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.file.bucket-name}")
    private String bucketName;

    public String uploadFileAvatar(MultipartFile file, String path)  throws Exception {

        String url = null;

        //创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        //获取上传文件输入流
        InputStream inputStream = file.getInputStream();
        //获取文件名称
        String fileName = file.getOriginalFilename();

        //保证文件名唯一，去掉uuid中的'-'
        String uuid = IdUtil.simpleUUID();
        fileName = uuid + fileName;

        //把文件按日期分类，构建日期路径：avatar/2019/02/26/文件名
        //String datePath = new Date().toString("yyyy/MM/dd");
        //拼接
        fileName = path + fileName;

        //调用oss方法上传到阿里云
        //第一个参数：Bucket名称
        //第二个参数：上传到oss文件路径和文件名称
        //第三个参数：上传文件输入流
        ossClient.putObject(bucketName, fileName, inputStream);

        //把上传后把文件url返回
        //https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
        url = "https://" + bucketName + "." + endpoint + "/" + fileName;
        //关闭OSSClient
        ossClient.shutdown();

        return url;
    }

    public boolean deleteFile(String fileName) {

        /**
         * 填写文件名。文件名包含路径，不包含Bucket名称。
         * 例如2021/09/14/52c6a3114e634979a2934f1ea12deaadfile.png。
         */
        String objectName = fileName;

        boolean flag = false;
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 删除文件。
            VoidResult voidResult = ossClient.deleteObject(bucketName, objectName);
            if (voidResult.getResponse().getStatusCode() == 204){
                flag = true;
            }
            // 关闭OSSClient。
            ossClient.shutdown();

        } catch (OSSException e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除指定的多个文件
     */
    public List<String> deleteFileList(List<String> fileList) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除请求
        DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName);
        // 赋值需要删除的文件
        deleteRequest.setKeys(fileList);
        //deleteRequest.setEncodingType(URL_ENCODING);
        // 调用删除
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteRequest);
        // 获取删除的文件
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
        // 关闭OSSClient。
        ossClient.shutdown();
        return deletedObjects;
    }
}
